<?php


namespace App\Rpc\client\impl;

use App\Common\Res;
use App\Common\StringUtils;
use App\Constants\ErrorCode;
use App\Rpc\client\DeviceInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine\MySQL;
use Hyperf\Config\Annotation\Value;


class DeviceImpl implements DeviceInterface
{
    use StringUtils, Res;

    /**
     * @Value("commonset.linkPlug.host")
     */
    protected $host;
    /**
     * @Value("commonset.linkPlug.user")
     */
    protected $user;

    /**
     * @Value("commonset.linkPlug.password")
     */
    protected $password;

    /**
     * @Value("commonset.linkPlug.database")
     */
    protected $database;

    /**
     * 获取设备状态列表
     * @param array $params
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function checkStatusList(array $params): array
    {
        $loginStatus = !empty($params['loginStatus']) ? intval($params['loginStatus']) : 1;
        $rawSql = $this->getDeviceSqlRew($loginStatus);
        $mysql = new MySQL();
        $mysql->connect([
            'host' => $this->host,
            'user' => $this->user,
            'password' => $this->password,
            'database' => $this->database,
            'charset' => 'utf8',
            'timeout' => 10,
        ]);
        if (!$mysql->connected) {
            // exit("connect failed. Error: {$mysql->error}\n");
            return $this->Error(ErrorCode::ERROR, "链接失败");
        }
        $result = $mysql->query($rawSql);
        if ($result === false) {
            //exit("query failed. Error: {$mysql->error}\n");
            return $this->Error(ErrorCode::ERROR, "查询失败");
        }
        $data = [];
        if (!empty($result)) {
            foreach ($result as $key => $value) {
                    $data[$key]['MOBILE'] = $value['MOBILE'];
                    $data[$key]['NICK_NAME'] = $value['NICK_NAME'];
                    $data[$key]['loginStatus'] = $value['loginStatus'];
            }
        }
        return $this->Success('获取设备状态列表成功', $data);
    }

}